﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using WeChat.PlatForm.Common;
using WeChat.PlatForm.Entity;

namespace WeChat.PlatForm.DataAccess
{
    public class MyDBContext : DbContext
    {
        public DbSet<PersonEntity> Persons { get; set; }

        public DbSet<SchoolEntity> Schools { get; set; }

        /// <summary>
        /// 角色
        /// </summary>
        public DbSet<RoleEntity> Roles { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        public DbSet<UserEntity> Users { get; set; }

        /// <summary>
        /// 权限
        /// </summary>
        public DbSet<UserRoleEntity> UserRoles { get; set; }

        /// <summary>
        /// 会员
        /// </summary>
        public DbSet<MemberEntity> Members { get; set; }
        
        /// <summary>
        /// 指定数据库
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            string con = AppConfig.GetConnction;
            optionsBuilder.UseMySql(con);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            Assembly assService = Assembly.Load(new AssemblyName("WeChat.PlatForm.DataAccess"));

            //杨中科老师自己封装的nuget包，其中的方法是ApplyConfigurationsFromAssembly
            modelBuilder.ApplyConfigurationsFromAssembly(assService);
        }
    }
}